package com.mysoft.gfyx.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysoft.gfyx.demo.dao.RoomDao;
import com.mysoft.gfyx.demo.dao.entity.Room;
import com.mysoft.gfyx.demo.service.RoomService;
import com.mysoft.gfyx.demo.service.dto.RoomDto;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.core.common.util.GuidGenerator;
import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.mybaits.extension.ExtensionLoadDataHelper;
import com.mysoft.framework.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoomServiceImpl extends AppService implements RoomService {

    @Autowired
    private RoomDao roomDao;

    @Override
    public FieldsSetting loadFields() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("wdGUID");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "java_wd_room";
        fieldsSetting.getFields().add(new DataApiField("wdGUID", groupName, "房间主键", FieldType.Guid));
        fieldsSetting.getFields().add(new DataApiField("x_area", groupName, "房间面积", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("x_building", groupName, "所属楼栋", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("x_environment", groupName, "景观", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("x_houseType", groupName, "户型", FieldType.Int));
        fieldsSetting.getFields().add(new DataApiField("x_orientation", groupName, "朝向", FieldType.Int));
        fieldsSetting.getFields().add(new DataApiField("x_owner", groupName, "户主姓名", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("x_price", groupName, "楼面价", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("x_roomNum", groupName, "房间号", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("x_saleTime", groupName, "销售时间", FieldType.DateTime));
        fieldsSetting.getFields().add(new DataApiField("x_status", groupName, "房间状态", FieldType.Int));
        // 返回
        return fieldsSetting;
    }

    @Override
    public ListDataResult loadData(LoadDataParams options) {
        // 分页
        Page<Room> page = ExtensionLoadDataHelper.getPage(options);
        QueryWrapper<Room> query = ExtensionLoadDataHelper.warp(Room.class, options);
        // 查询
        IPage<Room> roomPage = roomDao.selectPage(page, query);
        ListDataResult result = new ListDataResult();
        result.setData(MappingUtil.getSimpleMapperFacade().mapAsList(roomPage.getRecords(), RoomDto.class));
        result.setTotal((int) roomPage.getTotal());
        return result;
    }

    @Override
    public RoomDto saveOrUpdate(RoomDto dto) {
        Room room = MappingUtil.getSimpleMapperFacade().map(dto, Room.class);
        if (room.getRoomGUID() == null) {
            room.setRoomGUID(GuidGenerator.generateGuid());
            roomDao.insert(room);
        } else {
            roomDao.updateById(room);
        }
        return MappingUtil.getSimpleMapperFacade().map(room, RoomDto.class);
    }

    @Override
    public void deleteById(String id) {
        roomDao.deleteById(id);
    }

    @Override
    public RoomDto selectById(String id) {
        Room room = roomDao.selectById(id);
        return MappingUtil.getSimpleMapperFacade().map(room, RoomDto.class);
    }

    @Override
    public List<RoomDto> testWrap(String id) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("x_area", "100");
        List<Room> roomList = roomDao.selectList(queryWrapper);
        List<RoomDto> roomDtos = MappingUtil.getSimpleMapperFacade().mapAsList(roomList, RoomDto.class);
        return roomDtos;
    }

    @Override
    public List<RoomDto> testMapperXml(String id) {
        List<Room> roomList = roomDao.selectRoomByIds(new ArrayList<String>(){{
            add("100");
        }});
        List<RoomDto> roomDtos = MappingUtil.getSimpleMapperFacade().mapAsList(roomList, RoomDto.class);
        return roomDtos;
    }

}
